package cn.jiedh.app.meiweidk.mine;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.bigkoo.pickerview.OptionsPickerView;
import com.google.gson.Gson;

import org.xutils.ex.HttpException;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jiedh.app.meiweidk.MainActivity;
import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.CityUtils;
import cn.jiedh.app.meiweidk.Utils.UI.CustomItemLayout;
import cn.jiedh.app.meiweidk.Utils.event.EventScreen;
import cn.jiedh.app.meiweidk.Utils.event.EventUtils;
import cn.jiedh.app.meiweidk.Utils.tool.ActivityUtil;
import cn.jiedh.app.meiweidk.Utils.tool.JsonUtil;
import cn.jiedh.app.meiweidk.Utils.tool.L;
import cn.jiedh.app.meiweidk.Utils.tool.LocationUtil;
import cn.jiedh.app.meiweidk.Utils.tool.SPUtils;
import cn.jiedh.app.meiweidk.Utils.tool.StringUtil;
import cn.jiedh.app.meiweidk.Utils.tool.T;
import cn.jiedh.app.meiweidk.Utils.tool.UrlUtils;
import cn.jiedh.app.meiweidk.app.MyApp;
import cn.jiedh.app.meiweidk.base.BaseActivity;
import cn.jiedh.app.meiweidk.entity.LoginAndRegisterBean;
import cn.jiedh.app.meiweidk.entity.UserInfoBean;
import cn.jiedh.app.meiweidk.network.ResponseFailure;
import cn.jiedh.app.meiweidk.network.ResponseSuccess;
import cn.jiedh.app.meiweidk.response.BaseResponse;
import cn.jpush.android.api.JPushInterface;

/**
 * @name： 补充资料
 * @author：彭鑫
 * @email：pengwwe457621023@qq.com
 * @createTime：2018/11/17
 */

public class SuppleInfoActivity extends BaseActivity {

    @BindView(R.id.car_cil)
    CustomItemLayout car_cil;
    @BindView(R.id.house_cil)
    CustomItemLayout house_cil;
    @BindView(R.id.publicPrice_cil)
    CustomItemLayout publicPrice_cil;
    @BindView(R.id.social_cil)
    CustomItemLayout social_cil;
    @BindView(R.id.policy_cil)
    CustomItemLayout policy_cil;
    @BindView(R.id.card_cil)
    CustomItemLayout card_cil;
    @BindView(R.id.wcQuota_cil)
    CustomItemLayout wcQuota_cil;
    @BindView(R.id.socialcase_cil)
    CustomItemLayout socialcase_cil;


    private OptionsPickerView carPickerView;
    private int carType = 99;

    private OptionsPickerView housePickerView;
    private int houseType = 99;

    private OptionsPickerView publicPricePickerView;
    private int publicPriceType = 99;

    private OptionsPickerView socialPickerView;
    private int socialType = 99;

    private OptionsPickerView policyPickerView;
    private int policyType = 99;

    private OptionsPickerView cardPickerView;
    private int cardType = 99;

    private OptionsPickerView wcQuotaPickerView;
    private int wcQuotaType = 99;


    private OptionsPickerView socialcasePickerView;
    private int socialcaseType = 99;

    private UserInfoBean userInfoBean;


    @Override
    protected int getLayoutView() {
        return R.layout.activity_suppleinfo;

    }

    @Override
    protected void initView(View view) {
        setTitle("补充资料");
    }

    @Override
    protected void initData() {

        MyApp.getInstance().addActivity(this);

        userInfoBean = (UserInfoBean) getIntent().getExtras().getSerializable("userinfo");
    }


    /**
     * 构造pickerview
     *
     * @param itemLayout
     * @param pickerView
     * @param title
     * @param values
     */
    private void buildPicker(CustomItemLayout itemLayout, OptionsPickerView pickerView, String title, String... values) {

        if (values == null || values.length == 0) {
            return;
        }
        if (pickerView != null && pickerView.isShowing()) {
            pickerView.dismiss();
        }

        if (pickerView == null) {
            pickerView = new OptionsPickerView.Builder(this, (options1, options2, options3, v) -> {
                itemLayout.setValue(values[options1]);
                switch (itemLayout.getId()) {
                    case R.id.car_cil:
                        carType = options1;
                        L.i("carType===" + carType);
                        break;

                    case R.id.house_cil:
                        houseType = options1;
                        L.i("houseType===" + houseType);
                        break;

                    case R.id.publicPrice_cil:
                        publicPriceType = options1;
                        L.i("publicPriceType===" + publicPriceType);
                        break;

                    case R.id.social_cil:
                        socialType = options1;
                        L.i("socialType===" + socialType);
                        break;

                    case R.id.policy_cil:
                        policyType = options1;
                        L.i("policyType===" + policyType);
                        break;

                    case R.id.card_cil:
                        cardType = options1;
                        L.i("cardType===" + cardType);
                        break;

                    case R.id.wcQuota_cil:
                        wcQuotaType = options1;
                        L.i("wcQuotaType===" + wcQuotaType);
                        break;

                    case R.id.socialcase_cil:
                        socialcaseType = options1;
                        L.i("socialcaseType===" + socialcaseType);
                        break;

                }
            }).setOutSideCancelable(false)
                    .setDividerColor(R.color.hint3_text_color)
                    .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                    .setContentTextSize(20)
                    .setTitleText(title)
                    .setTitleColor(Color.BLACK)
                    .build();
            pickerView.setPicker(Arrays.asList(values));
        }
        pickerView.show();
    }


    @OnClick({R.id.authen_success, R.id.car_cil, R.id.house_cil, R.id.publicPrice_cil, R.id.social_cil, R.id.policy_cil, R.id.card_cil, R.id.wcQuota_cil, R.id.socialcase_cil})
    public void Click(View v) {
        switch (v.getId()) {
            case R.id.authen_success:
                if (checkData()) {
                    sendRequest();
                }
                break;
            case R.id.car_cil:
                buildPicker(car_cil, cardPickerView, "是否有车", PersonInfo.getCarTypes());
                break;

            case R.id.house_cil:
                buildPicker(house_cil, housePickerView, "是否有房", PersonInfo.getHouseTypes());
                break;
            case R.id.publicPrice_cil:
                buildPicker(publicPrice_cil, publicPricePickerView, "公积金", PersonInfo.getPublicPriceTypes());
                break;
            case R.id.social_cil:
                buildPicker(social_cil, socialPickerView, "社保", PersonInfo.getSocialTypes());
                break;
            case R.id.policy_cil:
                buildPicker(policy_cil, policyPickerView, "保单", PersonInfo.getPolicyTypes());
                break;
            case R.id.card_cil:
                buildPicker(card_cil, cardPickerView, "信用卡情况", PersonInfo.getCardTypes());
                break;
            case R.id.wcQuota_cil:
                buildPicker(wcQuota_cil, wcQuotaPickerView, "微粒贷额度", PersonInfo.getWcQuotaTypes());
                break;
            case R.id.socialcase_cil:
                buildPicker(socialcase_cil, socialcasePickerView, "社保缴纳情况", PersonInfo.getSocialCaseTypes());
                break;
        }
    }


    /**
     * 发送保存信息请求
     */
    private void sendRequest() {
        showLoadingDialog();
        Map<String, Object> map = JsonUtil.jsonToMap(JsonUtil.objectToJson(userInfoBean));
//        map.put("phone", phone);
//        map.put("sendSpreadSms", "0");
//        map.put("authCodePage", verification);
//        map.put("unite", 0);
        map.put("sendSpreadSms", "0");
        map.put("phone",SPUtils.getPhone());
        Double[] doubles = LocationUtil.location;
        if (doubles != null) {
            Double lat = doubles[0];
            Double lng = doubles[1];
            String latStr = String.valueOf(lat);
            String lngStr = String.valueOf(lng);
            map.put("longitude", latStr);
            map.put("latitude", lngStr);
        }


        request(UrlUtils.SAVE_USER_INFO, map, true, new ResponseSuccess<String>() {
            @Override
            public void onRequestSucceed(BaseResponse<String> response) {
                dismissLoadingDialog();
                T.showShort(response.getMessage());
                if (0 == response.getCode()) {//成功

                    PersonInfo info=new PersonInfo();
                    info.setCityLive(userInfoBean.getCityLive());
                    info.setUsername(userInfoBean.getUsername());
                    info.setIdCard(userInfoBean.getIdCard());

                    SPUtils.saveUserData(info);

                    MyApp.getInstance().finishAllActivity();
                }else{

                }
            }
        }, new ResponseFailure() {
            @Override
            public void onRequestFailure(HttpException error, String msg) {
                dismissLoadingDialog();
            }
        });
    }


    private boolean checkData() {
        if (carType == 99) {
            T.show("请选择是否有车");
            return false;
        } else if (houseType == 99) {
            T.show("请选择是否有房");
            return false;
        } else if (publicPriceType == 99) {
            T.show("请选择公积金情况");
            return false;
        } else if (socialType == 99) {
            T.show("请选择社保情况");
            return false;
        } else if (socialcaseType == 99) {
            T.show("请选择社保缴纳情况");
            return false;
        } else if (policyType == 99) {
            T.show("请选择保单情况");
            return false;
        } else if (cardType == 99) {
            T.show("请选择信用卡情况");
            return false;
        } else if (wcQuotaType == 99) {
            T.show("请选择微粒贷额度");
            return false;
        }

        userInfoBean.setCar(carType);
        userInfoBean.setHouse(houseType);
        userInfoBean.setFund(publicPriceType);
        userInfoBean.setSocial(socialType);
        userInfoBean.setSocialCase(socialcaseType);
        userInfoBean.setGuarantee(policyType);
        userInfoBean.setCreditCase(cardType);
        userInfoBean.setWeilidai(wcQuotaType);
        return true;
    }
}
